<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Basic usage</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="dba.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="ref.dba.html">DBA 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="dba.examples.html">范例</a></li>
    <li>Basic usage</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="dba.example" class="section">
  <h2 class="title">Basic usage</h2>
   <p class="para">
    <div class="example" id="example-999">
     <p><strong>示例 #1 DBA example</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dba_open</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/test.db"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"db2"</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"dba_open&nbsp;failed\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">dba_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"key"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;an&nbsp;example!"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">dba_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">"key"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">dba_fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">"key"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">dba_delete</span><span style="color: #007700">(</span><span style="color: #DD0000">"key"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">dba_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   DBA is binary safe and does not have any arbitrary limits.
   However, it inherits all limits set by the underlying
   database implementation.
  </p>
  <p class="para">
   All file-based databases must provide a way of setting the file
   mode of a new created database, if that is possible at all. The
   file mode is commonly passed as the fourth argument to
   <span class="function"><a href="function.dba-open.html" class="function">dba_open()</a></span> or <span class="function"><a href="function.dba-popen.html" class="function">dba_popen()</a></span>.
  </p>
  <p class="para">
   You can access all entries of a database in a linear way by using the
   <span class="function"><a href="function.dba-firstkey.html" class="function">dba_firstkey()</a></span> and <span class="function"><a href="function.dba-nextkey.html" class="function">dba_nextkey()</a></span>
   functions. You may not change the database while traversing it.
  </p> 
  <p class="para">
   <div class="example" id="example-1000">
    <p><strong>示例 #2 Traversing a database</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;...open&nbsp;database...<br /><br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dba_firstkey</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;remember&nbsp;the&nbsp;key&nbsp;to&nbsp;perform&nbsp;some&nbsp;action&nbsp;later<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$handle_later</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dba_nextkey</span><span style="color: #007700">(</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br />}<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$handle_later&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">dba_delete</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>